package cn.hyy.FVOnline.controller;


import cn.hyy.FVOnline.entity.Order;
import cn.hyy.FVOnline.entity.PageEntitys;
import cn.hyy.FVOnline.mapper.UserMapper;
import cn.hyy.FVOnline.mapper.GoodsMapper;
import cn.hyy.FVOnline.mapper.ShopMapper;
import cn.hyy.FVOnline.service.OrderService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/createOrder", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String createOrder(@RequestParam("userId") String userId,@RequestParam("goodsid") String goodsid,@RequestParam("shopid") String shopid,@RequestParam("addressid")String addressid,
                                            @RequestParam("buynum")String buynum, @RequestParam("pricesum") String pricesum, @RequestParam("remarks")String remarks){

        JSONObject jo = new JSONObject();

        try {
            Order order = new Order();
            order.setuId(Integer.parseInt(userId));
            order.setGoodsId(Integer.parseInt(goodsid));
            order.setShopId(Integer.parseInt(shopid));
            order.setAddressId(Integer.parseInt(addressid));
            order.setTradeNum(Integer.parseInt(buynum));
            order.setTradePrice(pricesum);
            if (remarks == null || remarks.length() == 0){
                order.setRemarks(null);
            }else {
                order.setRemarks(remarks);
            }

            Order create = orderService.createOrder(order);
            userMapper.addPay(Integer.parseInt(pricesum),Integer.parseInt(userId));
            goodsMapper.addSell(Integer.parseInt(buynum),Integer.parseInt(goodsid));
            shopMapper.addSell(Integer.parseInt(buynum),Integer.parseInt(shopid));
            jo.put("code","success");
            jo.put("message","提交订单成功");
            jo.put("order",create.getOrderNumber());
            jo.put("price",create.getTradePrice());



        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","提交订单失败");
        }

        return jo.toJSONString();
    }


    @RequestMapping(value = "/findByShopId", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String findByShopId(@RequestParam("shopid")String shopid,@RequestParam("page")String page,@RequestParam("pageSize")String pageSize){

        PageEntitys pageEntitys = orderService.findByShopId(Integer.parseInt(shopid), Integer.parseInt(page), Integer.parseInt(pageSize));

        return JSON.toJSONString(pageEntitys);
    }

    @Autowired
    private ShopMapper shopMapper;
    @RequestMapping(value = "/sendExpress", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String sendExpress(@RequestParam("id")String id, @RequestParam("expresstype") String expresstype, @RequestParam("expressnumber") String expressnumber){

        JSONObject jo = new JSONObject();

        try {
            Order order = new Order();
            order.setId(Integer.parseInt(id));
            order.setExpressName(expresstype);
            order.setExpressNumber(expressnumber);
            orderService.sendExpress(order);
            jo.put("code","success");
            jo.put("message","发货成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","发货失败！");
        }
        return jo.toJSONString();
    }


    @Autowired
    private GoodsMapper goodsMapper;
    @RequestMapping(value = "/findByShopIdTwo", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String findByShopIdTwo(@RequestParam("shopid")String shopid,@RequestParam("page")String page,@RequestParam("pageSize")String pageSize){

        PageEntitys pageEntitys = orderService.findByShopIdTwo(Integer.parseInt(shopid), Integer.parseInt(page), Integer.parseInt(pageSize));

        return JSON.toJSONString(pageEntitys);
    }


    @RequestMapping(value = "/findByUserId", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String findByUserId(@RequestParam("userId")String userId,@RequestParam("page")String page,@RequestParam("pageSize")String pageSize){

        PageEntitys pageEntitys = orderService.findByUserId(Integer.parseInt(userId), Integer.parseInt(page), Integer.parseInt(pageSize));

        return JSON.toJSONString(pageEntitys);
    }

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/receive", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String receive(@RequestParam("id")String id){

        JSONObject jo = new JSONObject();

        try {
            orderService.receive(Integer.parseInt(id));
            jo.put("code","success");
            jo.put("message","收货成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","收货失败！");
        }
        return jo.toJSONString();
    }


    @RequestMapping(value = "/findById", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String findById(@RequestParam("id")String id){
        Order order = orderService.findById(Integer.parseInt(id));
        return JSON.toJSONString(order);
    }



    @RequestMapping(value = "/evaluate", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String evaluate(@RequestParam("id")String id){

        JSONObject jo = new JSONObject();

        try {
            orderService.evaluate(Integer.parseInt(id));
            jo.put("code","success");
            jo.put("message","评价成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","评价失败！");
        }
        return jo.toJSONString();
    }

    @RequestMapping(value = "/getOrders", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getOrders(@RequestParam(value = "what",required = false)String what,@RequestParam(value = "page",required = false)String page,@RequestParam(value = "pageSize",required = false)String pageSize){
        PageEntitys orders = orderService.getOrders(what, Integer.parseInt(page), Integer.parseInt(pageSize));
        return JSONObject.toJSONString(orders);
    }


    @RequestMapping(value = "/getMessage", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getMessage(@RequestParam("uid")String uid){
        return JSON.toJSONString(orderService.getMessage(Integer.parseInt(uid)));
    }


    @RequestMapping(value = "/delMessage", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String delMessage(@RequestParam("uid")String uid){
        JSONObject jo = new JSONObject();

        try {
            orderService.delMessage(Integer.parseInt(uid));
            jo.put("code","message");
        }catch (Exception e){
            jo.put("code","error");
        }

        return jo.toJSONString();
    }
}
